#!/bin/bash
# Make a gnirehtet release.
#
# Put your keystore properties into ~/.gradle/gradle.properties
# (check app/build.gradle)

set -e # fail on error

GRADLE=${GRADLE:-./gradlew} # set value ./gradlew if not set
DIST=dist
TARGET_DIR_JAVA=gnirehtet-java
TARGET_DIR_RUST_LINUX=gnirehtet-rust-linux64
TARGET_DIR_RUST_WIN=gnirehtet-rust-win64

version=$(git describe --tags --always)
TARGET_JAVA="$TARGET_DIR_JAVA-$version".zip
TARGET_RUST_LINUX="$TARGET_DIR_RUST_LINUX-$version".zip
TARGET_RUST_WIN="$TARGET_DIR_RUST_WIN-$version".zip

"$GRADLE" checkAll releaseAll releaseRustWindows

rm -rf "$DIST"

for dir in "$TARGET_DIR_JAVA" "$TARGET_DIR_RUST_LINUX" "$TARGET_DIR_RUST_WIN"
do
    mkdir -p "$DIST/$dir"
    cp app/build/outputs/apk/release/gnirehtet-release.apk "$DIST/$dir"/gnirehtet.apk
done

cp relay-java/build/libs/gnirehtet.jar \
   relay-java/scripts/gnirehtet \
   relay-java/scripts/gnirehtet.cmd \
   relay-java/scripts/gnirehtet-run.cmd \
   "$DIST/$TARGET_DIR_JAVA"/
cp relay-rust/target/release/gnirehtet "$DIST/$TARGET_DIR_RUST_LINUX"/
strip "$DIST/$TARGET_DIR_RUST_LINUX"/gnirehtet  # remove symbols for smaller binary
cp relay-rust/target/x86_64-pc-windows-gnu/release/gnirehtet.exe \
   relay-rust/scripts/gnirehtet-run.cmd \
   "$DIST/$TARGET_DIR_RUST_WIN"/

cd "$DIST"
zip -r "$TARGET_JAVA" "$TARGET_DIR_JAVA"
zip -r "$TARGET_RUST_LINUX" "$TARGET_DIR_RUST_LINUX"
zip -r "$TARGET_RUST_WIN" "$TARGET_DIR_RUST_WIN"
sha256sum "$TARGET_JAVA" "$TARGET_RUST_LINUX" "$TARGET_RUST_WIN" > SHA256SUMS.txt
